﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Webknife.Data;

namespace ImportOrStuImg
{
    public partial class MainFrm : Form
    {
        delegate void SetLogCallback(string imgType, string logInfo);

        public MainFrm(){
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK){
                txtOutPut.Text = dialog.SelectedPath;
                WriteLog(ImgConst.SUCC, "初始化系统配置成功！");
            }
        }

        /// <summary>
        /// //C#读取文件夹在指定目录及子目录下查找文件,在listBox1中列出子目录及文件  
        /// </summary>
        /// <param name="dir">参数为指定的目录</param>
        public void FindFile(string dir)
        {
            DirectoryInfo Dir = new DirectoryInfo(dir);
            try
            {
                this.progressBar1.Maximum = Dir.GetFiles("*.*").Length;
                this.progressBar1.Minimum = 0;
                int num = 0;
                foreach (FileInfo accounts in Dir.GetFiles("*.*")) //查找文件  
                {
                    num++;
                    string usstuid;
                    if (stuOrexist(accounts.ToString(), out usstuid) == true){
                        strOrImg(usstuid, accounts.ToString());
                    }
                   this.progressBar1.Value = num;
                }
                WriteLog(ImgConst.SUCC, "入库完成！");
                MessageBox.Show("入库完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                button2.Enabled = true;
            }catch (Exception e){
                MessageBox.Show(e.Message);
            }   
        }
        public bool stuOrexist(string uid, out string usstuid)
        {
            string[] temp = uid.Split(new char[1] { '.' });
            string commandText = "select usstuid from us_student where accounts=" + temp[0] + "";
            DataTable table = DbHelper.ExecuteDataset(SysConst.DB_NAME, commandText).Tables[0];
            if (table != null & table.Rows.Count > 0){
                usstuid = table.Rows[0][0].ToString();
                return true;
            }else{
                SysLog.WriterFileError("不存在的学生"+commandText);
                WriteLog(ImgConst.WARN, "不存在的学生" + commandText);
                usstuid = "-1";
                return false;
            }
        }
        public bool strOrImg(string usstuid,string imgfile)
        {
            if (usstuid.Length == 0 || usstuid.EndsWith("-1")) return false;
            string commandText = "update stu_studentinfo set [image]='" + imgfile + "' where id=" + usstuid + "";
            try
            {
                DbHelper.ExecuteNonQuery(SysConst.DB_NAME, commandText);
                WriteLog(ImgConst.SUCC, imgfile.ToString());
                SysLog.WriterFileOK(commandText);
                return true;
            }catch {
                SysLog.WriterFileError(commandText);
                WriteLog(ImgConst.WARN, imgfile.ToString());
                return false;              
            }
        }
        private void WriteLog(string imgType, string logInfo)
        {
            ListViewItem infoMsg = new ListViewItem();
            infoMsg.ImageKey = imgType;
            infoMsg.Text = logInfo;
            infoMsg.ToolTipText = logInfo;
            infoMsg.SubItems.Add(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            if (hisView.InvokeRequired){
                SetLogCallback logCall = new SetLogCallback(WriteLog);
                this.Invoke(logCall, new object[] { imgType, logInfo });
            }else{
                hisView.Items.Add(infoMsg);
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {//入库
            button2.Enabled = false;
            FindFile(txtOutPut.Text);
            button2.Enabled = true;
        }
    }

    public class ImgConst
    {
        public const string INFO = "info";
        public const string SUCC = "succ";
        public const string WARN = "warn";

        public ImgConst()
        {
        }
    }
}
